package top.lyc829.zzl_android.ui.message.follow;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.graphics.Canvas;
import android.os.Bundle;

import com.chad.library.adapter.base.listener.OnItemSwipeListener;

import top.lyc829.zzl_android.R;
import top.lyc829.zzl_android.databinding.ActivityNoticeFollowBinding;
import top.lyc829.zzl_android.databinding.ActivityNoticePraiseBinding;
import top.lyc829.zzl_android.logic.bean.NoticeCommentBean;
import top.lyc829.zzl_android.logic.util.ReplayType;
import top.lyc829.zzl_android.logic.util.Utils;
import top.lyc829.zzl_android.ui.base_detail.BaseDetailActivity;
import top.lyc829.zzl_android.ui.message.praise.NoticePraiseActivity;
import top.lyc829.zzl_android.ui.message.praise.NoticePraiseAdapter;
import top.lyc829.zzl_android.ui.message.praise.NoticePraiseViewModel;
import top.lyc829.zzl_android.ui.profile.follow.user_detail.OtherUserDetailActivity;
import top.lyc829.zzl_android.ui.thing.dynamic_detail.DynamicDetailActivity;
import top.lyc829.zzl_android.ui.thing.travel_detail.TravelDetailActivity;

public class NoticeFollowActivity extends AppCompatActivity {

    ActivityNoticeFollowBinding binding;
    NoticeFollowViewModel mViewModel;
    NoticeFollowAdapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityNoticeFollowBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        mViewModel = new ViewModelProvider.AndroidViewModelFactory(getApplication()).create(NoticeFollowViewModel.class);

        binding.mTitleBarNoticeFollow.setLeftClickListener(v -> finish());

        initRecycler();

        observer();

        clickListener();

    }

    private void clickListener() {
        adapter.setOnItemClickListener((adapter1, view, position) -> {
            NoticeCommentBean comment = adapter.getItem(position);
            Intent intent = new Intent(this, OtherUserDetailActivity.class);
            intent.putExtra("id", comment.getUserId());
            startActivity(intent);
        });

    }

    private void observer() {

        mViewModel.getNoticeFollowUserLiveData().observe(this, noticeCommentBeans -> {
            adapter.setList(noticeCommentBeans);
        });
    }

    private void initRecycler() {

        // 侧滑监听
        OnItemSwipeListener onItemSwipeListener = new OnItemSwipeListener() {
            NoticeCommentBean commentBean = null;

            @Override
            public void onItemSwipeStart(RecyclerView.ViewHolder viewHolder, int pos) {
                commentBean = adapter.getItem(pos);
            }

            @Override
            public void clearView(RecyclerView.ViewHolder viewHolder, int pos) {
            }

            @Override
            public void onItemSwiped(RecyclerView.ViewHolder viewHolder, int pos) {
                Utils.Sout("View Swiped: " + pos);
                mViewModel.deleteItem(commentBean);
            }

            @Override
            public void onItemSwipeMoving(Canvas canvas, RecyclerView.ViewHolder viewHolder, float dX, float dY, boolean isCurrentlyActive) {
                canvas.drawColor(ContextCompat.getColor(NoticeFollowActivity.this, R.color.zzl_red));
            }
        };

        adapter = new NoticeFollowAdapter();
        binding.noticeFollowRecycler.setLayoutManager(new LinearLayoutManager(this));
        adapter.getDraggableModule().setSwipeEnabled(true);
        adapter.getDraggableModule().setOnItemSwipeListener(onItemSwipeListener);
        adapter.getDraggableModule().getItemTouchHelperCallback().setSwipeMoveFlags(ItemTouchHelper.START | ItemTouchHelper.END);
        binding.noticeFollowRecycler.setAdapter(adapter);
        adapter.setAnimationEnable(true);   // recyclerview加载动画
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (!Utils.isLogin()) {
            Utils.showToast("请先登录");
            //Utils.goLogin(this);
        } else {
            mViewModel.loadNoticeFollowUsers(Utils.getUserId());
        }
    }
}